﻿using System;
using System.Collections.Generic;
using System.Linq;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Mechanical;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YArchitech.Revit;

namespace YArchitech.HVAC
{
	public class BackConnOperation : ConnOperation
	{
		public BackConnOperation(UIDocument uiDoc, bool connPointSel) : base(uiDoc)
		{
			this.connPointSel = connPointSel;
		}

		public override bool SelectDucts()
		{
			if (!this.connPointSel)
			{
				bool flag = false;
				if (this.SelectDuctsMethod != null)
				{
					string errorString = "";
					IList<Duct> source = null;
					IList<XYZ> source2 = null;
					flag = this.SelectDuctsMethod.ConnDuctsSelecting(ConnDuctSelectType.BackConn, ref source, ref source2, ref errorString);
					if (flag)
					{
						base.SelectedDucts = source.ToList<Duct>();
						base.SelectedPoints = source2.ToList<XYZ>();
					}
					else
					{
						base.ErrorString = errorString;
					}
				}
				return flag;
			}
			ISelectionFilter selectionFilter = new DuctFilter();
			Reference reference = base.UIDoc.Selection.PickObject((Autodesk.Revit.UI.Selection.ObjectType)1, selectionFilter, "选择第一根风管……");
			base.SelectedDucts.Add(base.UIDoc.Document.GetElement(reference) as Duct);
			base.SelectedPoints.Add(reference.GlobalPoint);
			new HYDuctConnect();
			int ductShape = new ConnectDuctManage().GetDuctShape(base.SelectedDucts[0]);
			if (base.SelectedDucts.Count < 1)
			{
				base.ErrorString = "没有选中风管，请选择一根风管！";
				return false;
			}
			Reference reference2 = base.UIDoc.Selection.PickObject((Autodesk.Revit.UI.Selection.ObjectType)1, selectionFilter, "选择第二个风管,与第一根风管平行");
			base.SelectedDucts.Add(base.UIDoc.Document.GetElement(reference2) as Duct);
			if (!base.CheckDuctShape(base.SelectedDucts))
			{
				base.ErrorString = "选择的两根风管的截面类型必须相同！";
				return false;
			}
			if (ductShape == 1)
			{
				this.fittingStyle = ConnectDuctStyle.BackForth;
			}
			if (ductShape == 0)
			{
				this.fittingStyle = ConnectDuctStyle.BackForthRound;
			}
			if (this.fittingStyle == ConnectDuctStyle.BackForth && ductShape != 1)
			{
				base.ErrorString = "弯头截面与风管截面不匹配！";
				return false;
			}
			if (this.fittingStyle == ConnectDuctStyle.BackForthRound && ductShape != 0)
			{
				base.ErrorString = "弯头截面与风管截面不匹配！";
				return false;
			}
			return true;
		}

		public override bool Connect()
		{
			bool flag = false;
			if (base.SelectedDucts.Count < 1)
			{
				base.ErrorString = "选择的风管个数不对！";
				return flag;
			}
			HYDuctConnect hyductConnect = new HYDuctConnect();
			ConnectDuctManage connectDuctManage = new ConnectDuctManage();
			if (!connectDuctManage.BeSameShapeDucts(base.SelectedDucts))
			{
				base.ErrorString = "选择的风管截面必须一致！";
				return flag;
			}
			ConnectDuctStyle connStyle = base.ConnForm.connStyle;
			connectDuctManage.GetDuctShape(base.SelectedDucts[0]);
			string empty = string.Empty;
			if (!(base.UIDoc.Document.GetElementById(base.SelectedDucts[0].DuctType.RoutingPreferenceManager.GetRule((Autodesk.Revit.DB.RoutingPreferenceRuleGroupType)4, 0).MEPPartId) is FamilySymbol))
			{
				base.ErrorString = "布管配置中变径配置为空，请检查Revit的族配置";
				return flag;
			}
			List<string> fittingNames = connectDuctManage.GetFittingNames(connStyle);
			if (fittingNames.Count != 1)
			{
				base.ErrorString = "获取弯头族名称失败！";
				return false;
			}
			FamilySymbol familySymbol = connectDuctManage.GetFamilySymbol(fittingNames[0], base.UIDoc.Document, ref empty);
			if (familySymbol == null)
			{
				base.ErrorString = empty;
				return false;
			}
			IList<FamilyInstance> list = hyductConnect.NewLHWFitting(base.SelectedDucts, true, familySymbol, ref empty);
			if (list != null && list.Count > 0)
			{
				flag = true;
				base.UIDoc.Document.Regenerate();
			}
			if (!flag)
			{
				base.ErrorString = empty;
			}
			return flag;
		}

		private bool connPointSel;
	}
}
